package cn.iocoder.yudao.module.trade.service.followmerchant;

import cn.iocoder.yudao.module.system.api.merchant.MerchantUserApi;
import cn.iocoder.yudao.module.trade.dal.dataobject.followmerchant.SystemFollowMerchant;
import cn.iocoder.yudao.module.trade.dal.mysql.followmerchant.FollowMerchantMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 梁富贵
 * @Date 2024/10/17
 **/
@Service
public class FollowMerchantServiceImpl implements FollowMerchantService{

    @Resource
    private FollowMerchantMapper followMerchantMapper;

    @Resource
    private MerchantUserApi merchantUserApi;

    /**
     * 关注商家
     * @param merchantId 商家id
     * @return 是否关注
     */
    @Override
    public Boolean isFollow(Long merchantId) {
        if (!validateMerchantExist(merchantId)){
            return false;
        }
        SystemFollowMerchant systemFollowMerchant = new SystemFollowMerchant();
        systemFollowMerchant.setMerchantId(merchantId);
        LocalDateTime now = LocalDateTime.now();
        systemFollowMerchant.setCreateTime(now);
        return followMerchantMapper.isFollowMerchant(systemFollowMerchant) > 0;
    }

    /**
     * 取消关注商家
     * @param merchantId 商家id
     * @return 是否取消成功
     */
    @Override
    public Boolean unFollow(List<Long> merchantId) {
        return followMerchantMapper.deleteFollowMerchant(merchantId)>0;
    }

    private Boolean validateMerchantExist(Long merchantId){
        return merchantUserApi.validateMerchantExist(merchantId);
    }
}
